package com.ag.dgm.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.Session;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ag.dao.TbEventDAO;
import com.ag.dao.TbEventStatusDAO;
import com.ag.dao.TbUserDAO;
import com.ag.bean.TbEvent;
import com.ag.bean.TbEventReport;
import com.ag.bean.TbEventStatus;
import com.ag.bean.TbUser;
import com.ag.common.AjaxNotify;
import com.ag.common.BaseAction;
import com.ag.common.EventUtil;
import com.ag.common.IPermissionService;
import com.ag.dgm.service.IEventReportService;
import com.ag.lwc.service.EventStatuService;
import com.sdicons.json.validator.impl.predicates.Str;

/**
 * 
 * @author dgm
 * 针对dgm/EventReport.jsp页面的action
 *  
 */
@Controller("eventReportAction")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class EventReportAction extends BaseAction implements SessionAware {
	private static final long serialVersionUID = -5718117359507116634L;
	@Resource(name = "eventReportServiceImpl")
	private IEventReportService service;
	private TbUser tbUser;
	private TbEvent tbEvent;
	private String eventReportString; // 事件报告
	private Map<String, Object> sessionMap;
	private String result;
	private List<TbEventStatus> tbEventStatus;
	private String eventStatusIdString;
	private TbEventStatus tbEventStatus1;
	private String tbEventIdString; // 事件id也是事件报告id
	private String auditContent; // 事件审核内容
	private String auditAgree;
	private String username;    //用户名称，用来返回

	@Resource(name = "DGMtbEventStatus")
	private TbEventStatusDAO tbEventStatusDAO;

	/*
	 * 从万才那拿到EventStatusService
	 */
	@Resource(name = "EventStatuService")
	private EventStatuService eventStatuService;

	// 用来得到用户
	@Resource(name = "PermissionServiceImpl")
	private IPermissionService pms;

	public String getUsername() {
		return username;
	}

	/* 测试用的 */
	private String hqlString;
	@Resource(name = "DGMtbUserDAO")
	private TbUserDAO tbUserDAO;
	@Resource(name = "DGMtbEventDAO")
	private TbEventDAO tbEventDAO;

	public String getAuditAgree() {
		return auditAgree;
	}

	public void setAuditAgree(String auditAgree) {
		this.auditAgree = auditAgree;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getEventStatusIdString() {
		return eventStatusIdString;
	}

	public IPermissionService getPms() {
		return pms;
	}

	public String getAuditContent() {
		return auditContent;
	}

	public void setAuditContent(String auditContent) {
		this.auditContent = auditContent;
	}

	public void setPms(IPermissionService pms) {
		this.pms = pms;
	}

	public void setEventStatusIdString(String eventStatusIdString) {
		this.eventStatusIdString = eventStatusIdString;
	}

	public List<TbEventStatus> getTbEventStatus() {
		return tbEventStatus;
	}

	public void setTbEventStatus(List<TbEventStatus> tbEventStatus) {
		this.tbEventStatus = tbEventStatus;
	}

	public String getResult() {
		return result;
	}

	public String getTbEventIdString() {
		return tbEventIdString;
	}

	public void setTbEventIdString(String tbEventIdString) {
		this.tbEventIdString = tbEventIdString;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getEventReportString() {
		return eventReportString;
	}

	public void setEventReportString(String eventReportString) {
		this.eventReportString = eventReportString;
	}

	public TbUser getTbUser() {
		return tbUser;
	}

	public void setTbUser(TbUser tbUser) {
		this.tbUser = tbUser;
	}

	public IEventReportService getService() {
		return service;
	}

	public void setService(IEventReportService service) {
		this.service = service;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.sessionMap = session;
	}

	/**
	 * 得到用户信息
	 */

	public TbUser findUser() {
		System.out.println("这是findUser_eventReportAction方法");
		tbUser = (TbUser) pms.getCurrentUser(sessionMap);
		return tbUser;
	}

	/**
	 * 
	 * 得到tbEvent,现在直接会传进参数进来
	 */
	public TbEvent findTbEvent() {
		System.out.println("这是findTbEvent_eventReportAction方法");
		// tbEventIdString="1008011002";
		tbEvent = (TbEvent) tbEventDAO.findById(tbEventIdString);
		return tbEvent;
	}

	/**
	 * 提交办结报告,不改变状态
	 */

	public String submitEventReportString() {
		System.out.println("这是submitEventReportString_eventReportAction方法");
		this.setResult("办结成功");
		try {
			tbUser = findUser();
		} catch (Exception e) {
			this.setResult("错误用户");
		}
		try {
			tbEvent = findTbEvent();		//找到当前事件
			if (tbEvent == null) {
				System.out.println("tbEvent为空！");
			}
		} catch (Exception e) {
			this.setResult("错误事件");
		}
		try {
			service.submitEventReport(tbUser, eventReportString, tbEvent);	//提交办结报告
		} catch (Exception e) {
			this.setResult("办结错误");
		}

		System.out.println(result);
		return "submitEventReportString_succ";
	}

	public String loadEventReportString() {
		tbEvent = findTbEvent();
		// System.out.println("((((((((((((((((("+tbEvent.getEId());
		eventReportString = service.loadEventReportString(tbEvent);
		return "loadEventReportString_succ";
	}

	/*
	 * 用来归档的
	 */
	public String settheEnd() {
		this.setResult("归档成功");
		try {
			TbUser tbUser = findUser();
		} catch (Exception e1) {
			this.setResult("错误用户");
			e1.printStackTrace();
		}
		try {
			System.out.println("执行settheEnd方法");
			service.updateEventEnd(tbEventIdString,tbUser);
		} catch (Exception e) {
			this.setResult("归档失败");
		}
		try {
			service.deleteEventcommander(tbEventIdString);
		} catch (Exception e) {
			result="指挥长删除失败";
			e.printStackTrace();
		}
		return "settheEnd_succ";
	}

	/*
	 * 用来办结审核的,事件状态由"处置完"变为"办结"
	 */
	public String auditReport() {
		System.out.println("这是auditReport_Action方法");
		this.setResult("提交审核成功");
		tbUser = this.findUser();
		username=tbUser.getUName();
		try {
			service.auditReport(auditContent, tbUser, tbEventIdString,auditAgree);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result="提交审核失败";
			e.printStackTrace();
		}
		return "auditReport_succ";
	}
	/*
	 * 查询审核报告，未完成
	 */
	
	public String findEventReportAudit(){
		System.out.println("这是findEventReportAudit_Action方法");
		result = "查找成功";
		return "findEventReportAudit_succ";
	}

	
	/**
	 * 判断事件是否合条件
	 * @return
	 */
	public String checkStatusByEventId(){
		System.out.println("这是checkStatusByEventId_action方法");
		boolean bl = service.checkStatus(tbEventIdString);
		if(bl){
			result="Y";
		}else{
			result="N";
		}
		return "checkStatusByEventId_succ";
	}
	/*
	 * 用来取得事件状态的list
	 */
	/*
	 * public String findEventStatus() { tbEventStatus =
	 * eventStatuService.findAll(); // System.out.println(tbEventStatus.size());
	 * return "findEventStatus"; }
	 */

}
